var express=require("express");
var fs=require("fs");
var query=require("querystring");
var http=express();
http.listen(6500,function () {
    console.log(1)
});
http.use(function (req,res,next) {
    res.header("Access-Control-Allow-Origin","*");
    next();
});


//验证手机号是否重复注册
http.get("/isrepeat",function (req,res) {
    var tel=req.query.tel;
    var bol=true;
    fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(tel==arr[i].tel){
                bol=false;
                res.send("0")
            }
        }
        if(bol){
            res.send("1")
        }
    })

});


//验证码接口
http.post("/yzm",function (req,res) {
    parseInt(Math.random()*9);
    res.send(""+parseInt(Math.random()*9)+parseInt(Math.random()*9) +parseInt(Math.random()*9)+parseInt(Math.random()*9))
});


//校验码接口
http.post("/xym",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        var bol=true;
        var num=""+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9) +parseInt(Math.random()*9);
    })
    fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        for(var i=0;i<arr.length;i++){
            if(arr[i].tel==dataObj.tel){
                arr[i].xym=num;
                arr[i].time=dataObj.time;
                bol=false;
                fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),"utf-8",function () {
                    res.send(num);
                })
            }
        }
        if(bol){
            dataObj.xym=num;
            arr.push(dataObj);
            fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),"utf-8",function () {
                res.send(num);
            })
        }
    })
});


//注册
http.post("/adduser",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    })
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
            var arr=JSON.parse(data);
            for(var i=0;i<arr.length;i++){
                if(dataObj.tel==arr[i].tel){
                    if(dataObj.time-arr[i].time>=15000){
                        res.send("0")
                    }else if(dataObj.xym==arr[i].xym){
                        fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
                            var arr1=JSON.parse(data);
                            var obj={
                                id:dataObj.time,
                                user:dataObj.tel,
                                pass:dataObj.pwd
                            };
                            arr1.push(obj);
                            fs.writeFile("./src/data/userlist.json",JSON.stringify(arr1),"utf-8",function () {
                                res.send("1")
                            })
                        })
                    }else {
                        res.send("-1")
                    }
                }
            }
        })
    })
});

//排序接口
http.get("/px",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/shop1.json","utf-8",function (err,data) {
        var arr=JSON.parse(data).shop_data;
        var backarr=[];
        var num=arr.length;
        if(dataObj.type==""){
            res.send(arr);
            return;
        }
        for(var i=0;i<num;i++){
            var max=arr[0][dataObj.type];
            var index=0;
            for(var j=0;j<arr.length;j++){
                if(max*1<arr[j][dataObj.type]*1){
                    max=arr[j][dataObj.type];
                    index=j;
                }
            }
            backarr.push(arr[index]);
            arr.splice(index,1)
        }
        res.send(backarr)
    })
})


http.all("*",function (req,res) {
    res.sendFile(__dirname+req.url)
});